import requests
import json  # 添加导入json模块
import pandas as pd

# 百度热搜榜地址
url = 'https://top.baidu.com/api/board?platform=wise&tab=realtime'

# 构造请求头
headers = {  # 修改为 headers
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Mobile Safari/537.36',
    'Host': 'top.baidu.com',
    'Accept': 'application/json, text/plain, */*',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7',
    'Accept-Encoding': 'gzip, deflate, br',
    'Referer': 'https://top.baidu.com/board?tab=novel',
}
result_str = "【百度热点】"
# 发送请求
response = requests.get(url, headers=headers)  # 修改为 headers

# 解析JSON数据
json_data = response.json()

# 爬取置顶热搜
top_content_list = json_data['data']['cards'][0]['topContent']
print("0.",top_content_list[0]["query"])
result_str += f'\n0. {top_content_list[0]["query"]}'
# 爬取普通热搜
content_list = json_data['data']['cards'][0]['content']


# 使用计数器变量追踪行数
count = 1
for i in content_list[:15]:
    print(f"{count}. {i['query']} {i['hotScore']}")
    result_str += f"\n{count}. {i['query']} {i['hotScore']}"
    count += 1


# #全量打印：标题、热度、简介、新闻地址
# for i in content_list:
#     print(i["query"], i["hotScore"],i["desc"],i["appUrl"])

#发送vx消息
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d230f478-24eb-4780-a45d-0e8fe951bded'
headers = {'Content-Type': 'application/json'}
#文本
#带超链接
#article ="<a href=\"http://baidu.com\">邮件中心视频实况</a>"


data = {
    "msgtype": "text",
    "text": {
        "content": result_str
    },
    "safe": 0,
    "enable_id_trans": 0,
    "enable_duplicate_check": 0,
    "duplicate_check_interval": 1800
}
response = requests.post(url, headers=headers, data=json.dumps(data))
